package com.zmn.plat.admin.controller.extension.serv.category.group;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.model.entity.category.BaseCategory;
import com.zmn.plat.model.entity.extension.category.ExtensionCategory;
import com.zmn.plat.model.entity.extension.category.ExtensionCategoryQuery;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategory;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategoryAssociation;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategoryAssociationQuery;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategoryQuery;
import com.zmn.plat.model.entity.extension.serv.category.group.ExtensionServCategoryGroupQuery;
import com.zmn.plat.model.vo.extension.serv.category.ExtensionServCategoryAssociationVO;
import com.zmn.plat.model.vo.extension.serv.category.group.ExtensionServCategoryGroupVO;
import com.zmn.plat.model.vo.servcategory.ServCategoryVO;

/**
 * 服务推广分类-推广分类关联
 *
 * @author linfeng
 * @since 2019-12-24 10:55:48
 */
@Controller
@RequestMapping("/extension/serv/category/group/")
public class ExtensionServCategoryGroupListController extends ExtensionServCategoryGroupBaseController {
    /**
     * 详情
     *
     * @param servCategId
     * @author linfeng
     * @since 2019-12-24 10:55:48
     */
    @RequestMapping("detail")
    @RequiresPermissions("/extension/serv/category/group/detail.action")
    public ModelAndView detail(@RequestParam(value = "servCategId", defaultValue = "0") Integer servCategId,
        @RequestParam(value = "extensionServCategName", defaultValue = "") String extensionServCategName) {
        ExtensionServCategoryGroupQuery extensionServCategoryGroupQuery = new ExtensionServCategoryGroupQuery();
        extensionServCategoryGroupQuery.setExtensionServCategId(servCategId);
        List<ExtensionServCategoryGroupVO> extensionServCategoryGroups =
            extensionServCategoryGroupService.listByQuery(extensionServCategoryGroupQuery);

        ExtensionCategoryQuery extensionCategoryQuery = new ExtensionCategoryQuery();
        extensionCategoryQuery.setStatus(GlobalConsts.YES);
        List<ExtensionCategory> extensionCategories = extensionCategoryService.listByQuery(extensionCategoryQuery);

        List<Integer> relationGroupIds = extensionServCategoryGroups.stream().map(e -> e.getCategId()).collect(Collectors.toList());
        extensionCategories.forEach(item -> {
            if (relationGroupIds.contains(item.getCategId())) {
                item.setStatus(GlobalConsts.YES);
            } else {
                item.setStatus(GlobalConsts.NO);
            }
        });

        ModelAndView mav = new ModelAndView("extension/serv/category/group/detail");
        mav.addObject("extensionCategories", extensionCategories);
        mav.addObject("servCategId", servCategId);
        mav.addObject("extensionServCategName", extensionServCategName);
        return mav;
    }

    @RequestMapping(value = "list")
    @RequiresPermissions("/extension/serv/category/group/list.action")
    public ModelAndView list(@ModelAttribute ExtensionServCategoryGroupQuery query) {
        List<ExtensionServCategoryGroupVO> items = extensionServCategoryGroupService.listPageByQuery(query);
        List<ExtensionServCategory> extensionServCategories = extensionServCategoryService.listByQuery(new ExtensionServCategoryQuery());
        List<ExtensionServCategoryGroupVO> extensionServCategoryGroupVOS = new ArrayList<>();
        List<ExtensionCategory> extensionCategorys = new ArrayList<>();
        List<ExtensionCategory> result = new ArrayList<>();
        if (query.getExtensionServCategId() != null) {
            ExtensionServCategoryGroupQuery extensionServCategoryGroupQuery = new ExtensionServCategoryGroupQuery();
            extensionServCategoryGroupQuery.setExtensionServCategId(query.getExtensionServCategId());
            extensionServCategoryGroupVOS = extensionServCategoryGroupService.listByQuery(extensionServCategoryGroupQuery);
            extensionServCategoryGroupVOS.forEach(e -> {
                ExtensionCategory extensionCategory = new ExtensionCategory();
                extensionCategory.setCategId(e.getCategId());
                extensionCategory.setName(e.getExtensionCategName());
                result.add(extensionCategory);
            });
            extensionCategorys = result;
        } else {
            extensionCategorys = extensionCategoryService.listAll();
        }

        ModelAndView mav = new ModelAndView("extension/serv/category/group/list");
        List<ExtensionServCategoryAssociationVO> extensionServCategoryAssociationVOList = extensionServCategoryAssociationService
            .listByExtensionServCategoryId(query.getExtensionServCategId(), query.getCategId(), null);
        Map<String, List<ExtensionServCategoryAssociationVO>> map = extensionServCategoryAssociationVOList.stream()
            .collect(Collectors.groupingBy(ExtensionServCategoryAssociationVO::getRelateKey));

        mav.addObject("extensionServCategs", extensionServCategories);
        mav.addObject("extensionCategs", extensionCategorys);
        mav.addObject("vo", query);
        mav.addObject("items", items);
        mav.addObject("map", map);

        return mav;
    }

    @RequestMapping(value = "relateBackCategory")
    @RequiresPermissions("/extension/serv/category/group/relateBackCategory.action")
    public ModelAndView relateBackCategory(@RequestParam(value = "groupId", defaultValue = "0") Integer groupId,
        @RequestParam(value = "extensionServCategId", defaultValue = "0") Integer extensionServCategId,
        @RequestParam(value = "extensionCategId", defaultValue = "0") Integer extensionCategId,
        @RequestParam(value = "extensionServCategName", defaultValue = "") String extensionServCategName,
        @RequestParam(value = "extensionCategName", defaultValue = "") String extensionCategName) {
        List<BaseCategory> categOnes = baseCategoryService.listByParentId(GlobalConsts.TOP_ID, GlobalConsts.YES);
        List<ServCategoryVO> servAssCategoryByServId =
            servAssCategoryService.findServAssCategoryByServIdAndShowType(extensionServCategId, ProductConsts.ERP_PRODUCT_TYPE);
        ExtensionServCategoryAssociationQuery extensionServCategoryAssociationQuery = new ExtensionServCategoryAssociationQuery();
        extensionServCategoryAssociationQuery.setExtensionServCategId(extensionServCategId);
        extensionServCategoryAssociationQuery.setExtensionCategId(extensionCategId);
        List<ExtensionServCategoryAssociation> extensionServCategoryAssociationList =
            extensionServCategoryAssociationService.listByQuery(extensionServCategoryAssociationQuery);
        Map<Integer, List<ServCategoryVO>> map =
            servAssCategoryByServId.stream().collect(Collectors.groupingBy(ServCategoryVO::getCategOneId));

        for (Map.Entry<Integer, List<ServCategoryVO>> entry : map.entrySet()) {
            for (ServCategoryVO servCategoryVo : entry.getValue()) {
                for (ExtensionServCategoryAssociation extensionServCategoryAssociation : extensionServCategoryAssociationList) {
                    if (extensionServCategoryAssociation.getCategId().equals(servCategoryVo.getCategId())) {
                        servCategoryVo.setRelated(true);
                        break;
                    } else {
                        servCategoryVo.setRelated(false);
                    }
                }
            }
        }

        ModelAndView mav = new ModelAndView("extension/serv/category/group/relatebackcategory");
        mav.addObject("extensionServCategId", extensionServCategId);
        mav.addObject("extensionCategId", extensionCategId);
        mav.addObject("extensionServCategName", extensionServCategName);
        mav.addObject("extensionCategName", extensionCategName);
        mav.addObject("items", servAssCategoryByServId);
        mav.addObject("categOnes", categOnes);
        mav.addObject("map", map);
        return mav;
    }

    /**
     * 描述：分类联动（推广服务分类查询关联的推广分类）
     * 
     * @param extensionServCategId
     *            推广服务分类ID
     */
    @RequestMapping(value = "listChilds")
    public void listChilds(HttpServletResponse response,
        @RequestParam(value = "extensionServCategId", defaultValue = "1") int extensionServCategId) {
        super.loadListChilds(response, extensionServCategId);
    }

}
